package com.hutou.chuangke.activity;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Checkable;
import android.widget.Checkable;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.JsonObject;
import com.hutou.chuangke.Bean.UserBean;
import com.hutou.chuangke.MainApplication;
import com.hutou.chuangke.R;
import com.hutou.chuangke.dialog.NetDailog;
import com.hutou.chuangke.net.HttpUtil;
import com.hutou.chuangke.net.NetworkUtils;
import com.hutou.chuangke.net.http.User;
import com.hutou.chuangke.uitl.ActivityManager;
import com.hutou.chuangke.uitl.CommonUtils;
import com.hutou.chuangke.uitl.LogUtil;
import com.hutou.chuangke.uitl.SharedPreferencesUtil;
import com.hutou.chuangke.uitl.Utils;

/**
 * Created by tyhdark on 2017/3/21.
 * 登录界面
 */
public class LoginActivity extends AppCompatActivity implements View.OnClickListener, CompoundButton.OnCheckedChangeListener {
    private String TAG = "LoginActivity";
    private boolean isFromCache = true;
    TextView right_tv;
    ImageView back_image;
    Button loginBT;
    ImageView wechatImageview,qqImageview,weiboImageview;
    TextView forgetpwTv;
    CheckBox eyeCheckBox;
    EditText passwordET,accountET;
    private String password,account;
    SharedPreferencesUtil sharedPreferencesUtil;
    Context mContext;
    UserBean user;
    NetDailog netDailog;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ActivityManager.getInstatnce().addActivity(this);//统一管理activity
        mContext = LoginActivity.this;
        initView();
    }

    private void initView() {
        netDailog = new NetDailog(mContext);
        sharedPreferencesUtil = new SharedPreferencesUtil();
        right_tv = (TextView) findViewById(R.id.right_tv);
        right_tv.setOnClickListener(this);
        back_image = (ImageView) findViewById(R.id.back_image);
        back_image.setOnClickListener(this);
        loginBT = (Button) findViewById(R.id.login_bt);
        wechatImageview = (ImageView) findViewById(R.id.wechat_imageview);
        wechatImageview.setOnClickListener(this);
        weiboImageview = (ImageView) findViewById(R.id.weibo_imageview);
        weiboImageview.setOnClickListener(this);
        qqImageview = (ImageView) findViewById(R.id.qq_imageview);
        qqImageview.setOnClickListener(this);
        forgetpwTv = (TextView) findViewById(R.id.forgetpw_textview);
        forgetpwTv.setOnClickListener(this);
        forgetpwTv.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG); //下划线
        forgetpwTv.getPaint().setAntiAlias(true);//抗锯齿
        eyeCheckBox = (CheckBox) findViewById(R.id.eye_checkbox);
        eyeCheckBox.setOnCheckedChangeListener(this);
        accountET = (EditText) findViewById(R.id.account_et);
        passwordET = (EditText) findViewById(R.id.password_et);
        loginBT.setOnClickListener(this);
    }

    @Override
    protected void onStart() {
        super.onStart();
        account = (String) sharedPreferencesUtil.getData(mContext,
                SharedPreferencesUtil.USER_NAME,"");
        password = (String) sharedPreferencesUtil.getData(mContext,
                SharedPreferencesUtil.PASS_WORD,"");
        if(!account.isEmpty()){
            accountET.setText(account);
        }
        if(!password.isEmpty()){
            passwordET.setText(password);
        }
    }

    @Override
    public void onClick(View v) {
        if(v == wechatImageview){

        }else if(v == weiboImageview){

        }else if(v == qqImageview){

        }else if(v == forgetpwTv){

        }else if(v == loginBT){
            if(accountET.getText().toString().isEmpty()){
                Toast.makeText(mContext,"请输入手机号",Toast.LENGTH_SHORT).show();
                return;
            }else if(passwordET.getText().toString().isEmpty()){
                Toast.makeText(mContext,"请输入密码",Toast.LENGTH_SHORT).show();
                return;
            }
            account = accountET.getText().toString();
            password = passwordET.getText().toString();
            netDailog.show();
            loginRequest();
        }else if(v == back_image){
            Intent intent = new Intent(LoginActivity.this,LoginRegisterActivity.class);
            startActivity(intent);
            finish();
        }else if(v == right_tv){
            Intent intent = new Intent(LoginActivity.this,RegisterActivity.class);
            startActivity(intent);
        }
    }

    private void loginRequest(){
        new AsyncTask<Void,Void,Integer>(){

            @Override
            protected Integer doInBackground(Void... voids) {
                int msg = 0;
                try{
                    String Sid = Utils.md5Password(account+password+"ZK");
                    JsonObject list = HttpUtil.getResposeJsonObject(User.login(account,password,Sid));
                    if(list==null){
                        return 0;
                    }
                    user = MainApplication.gsonInstance().fromJson(list,UserBean.class);
                    msg = Integer.valueOf(user.getMsg());
                    LogUtil.d(TAG,"MSG="+msg);
                    String flag = user.getFlag();
                    LogUtil.d(TAG,"flag="+flag);

                }catch(Exception e){
                    e.printStackTrace();
                    return 0;
                }
                return msg;
            }

            @Override
            protected void onPostExecute(Integer msg) {
                super.onPostExecute(msg);
                netDailog.dismiss();
                if(msg==1) {//登陆成功
                    sharedPreferencesUtil.saveData(mContext,SharedPreferencesUtil.USER_NAME,account);
                    sharedPreferencesUtil.saveData(mContext,SharedPreferencesUtil.PASS_WORD,password);
//                    Intent intent = new Intent();
//                    intent.putExtra("user",user);
//                    LoginActivity.this.setResult(1,intent);
                    setResult(Activity.RESULT_OK);
//                    finish();
                    ActivityManager.getInstatnce().finishAllActivity();
                }else if(msg == 11){//账号密码错误
                    Toast.makeText(mContext,"表示用户名或者密码错误",Toast.LENGTH_SHORT).show();
                }
            }
        }.execute();
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        if(buttonView == eyeCheckBox) {
            if (eyeCheckBox.isChecked()) {
                passwordET.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
            }else {
                passwordET.setTransformationMethod(PasswordTransformationMethod.getInstance());
            }
        }
    }
}
